//
//  ShareBoxView.m
//  TestShareBoxView
//
//  Created by yutao on 13-11-17.
//  Copyright (c) 2013年 yutao. All rights reserved.
//

#import "SNShareBoxView.h"
#define kBoxViewHeight  280


@interface SNShareItemView ()
@property (nonatomic, copy)   NSString *text;

@property (nonatomic, strong) UIImage *image;
@end

@implementation SNShareItemView

@synthesize delegate = _delegate;
- (id)initWithFrame:(CGRect) frame text:(NSString *)text image:(UIImage *)image index:(int)index
{
    self = [super initWithFrame:frame];
    if (self) {
        UILabel  * textLabel = [[UILabel alloc]init];
        textLabel.text = text;
        [textLabel setFrame:CGRectMake(0, 63, 70, 13)];
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.font = [UIFont systemFontOfSize:12];
        textLabel.textAlignment = NSTextAlignmentCenter;
        
        UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [imageButton setImage:image forState:UIControlStateNormal];
        imageButton.tag = kButtonTagBase + index;
        [imageButton setFrame:CGRectMake(6.5, 0, 57, 57)];
        [imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:textLabel];
        [self addSubview:imageButton];
    }
    return self;
    
}

- (void)buttonClicked:(UIButton*)button{
    if ([self.delegate respondsToSelector:@selector(shareItemViewDidClick:)]) {
        [self.delegate shareItemViewDidClick:button];
    }
}

@end


@interface SNShareBoxView (){
    UIView * boxView;
}

@end

@implementation SNShareBoxView
- (id)initWithFrame:(CGRect)frame shareArray:(NSMutableArray*)shareArr viewController:(UIViewController*)viewController type:(SNShareBoxType)aType
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        
        boxView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds), 320, CGRectGetHeight(self.bounds)-kBoxViewHeight)];
        [boxView setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:boxView];
        
        UILabel * titleLable = [[UILabel alloc] initWithFrame:CGRectMake(80, 10, CGRectGetHeight(self.bounds)/3 , 40)];
        if (aType == SNShareBoxTypeLogin) {
            [titleLable setText:@"绑定账号"];
        }else{
            [titleLable setText:@"分享快乐"];
        }
        
        [titleLable setBackgroundColor:[UIColor clearColor]];
        [titleLable setTextAlignment:NSTextAlignmentCenter];
        [boxView addSubview:titleLable];
        
        for (int i = 0; i< [shareArr count]; i ++) {
            SNShareItemView * buttonView = [[SNShareItemView alloc] initWithFrame:CGRectMake(20+i*70+i*35, CGRectGetMaxY(titleLable.frame)+10, 70, 70) text:[[shareArr objectAtIndex:i] objectForKey:kTextKey] image:[UIImage imageNamed:[[shareArr objectAtIndex:i] objectForKey:kImgKey]] index:i+1];
            [buttonView setDelegate:(id)viewController];
            [boxView addSubview:buttonView];
        }
        
        UIButton * cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancleButton setFrame:CGRectMake(80, CGRectGetHeight(boxView.frame)- 50, CGRectGetHeight(self.bounds)/3 , 40)];
        [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancleButton addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
        [boxView addSubview:cancleButton];
        
        UITapGestureRecognizer * tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidden)];
        [self addGestureRecognizer:tap];
        
    }
    return self;
}

- (void)show{
    [UIView animateWithDuration:0.25 animations:^{
        [boxView setFrame:CGRectMake(0, kBoxViewHeight, 320, CGRectGetHeight(self.bounds)-kBoxViewHeight)];
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    }];
}

- (void)hidden
{
    [UIView animateWithDuration:0.25 animations:^{
        [boxView setFrame:CGRectMake(0, CGRectGetHeight(self.bounds), 320, CGRectGetHeight(self.bounds)-200)];
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    } completion:^(BOOL finished) {
        [boxView removeFromSuperview];
        [self removeFromSuperview];
    }];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
